package tech.wetech.admin3.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin3.sys.service.AooQueryService;
import tech.wetech.admin3.util.CustomResponse;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/xu_ni_ren/query")
public class AooQueryController {
  private AooQueryService queryService;

  public AooQueryController(AooQueryService queryService){
    this.queryService=queryService;
  }

  @GetMapping("/data")
  public ResponseEntity<?> queryAll(
    @RequestParam("email") String email,
    @RequestParam("paid") boolean paid
  ){
    List<Object> dataList = queryService.getDataByEmail(email); // 获取email用户的所有data
    List<Object> filteredDataList = dataList.stream()
      .filter(data -> {
        // 检查对象是否有getPaid方法并调用它
        try {
          Method getPaidMethod = data.getClass().getMethod("isPaid");
          System.out.println("getPaidMethod.invoke(data): " + getPaidMethod.invoke(data));
          return getPaidMethod.invoke(data).equals(paid);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
          return false;
        }
      })
      .collect(Collectors.toList());
    return ResponseEntity.ok(new CustomResponse.r1("ok", true, filteredDataList));
  }

  @GetMapping("/data/{email}")
  public ResponseEntity<?> queryByDigitalName(
    @PathVariable("email") String email,
    @RequestParam("digitalName") String digitalName
  ){
    List<Object> dataList = queryService.getDataByEmailAndDigitalName(email, digitalName);   // 获取email用户的指定姓名的data
    return ResponseEntity.ok(new CustomResponse.r1("ok", true, dataList));
  }

}
